

#include "Notes.h"


/************************************************************************

    FUNCTION:   main

 ************************************************************************/


JNIEXPORT jlong JNICALL Java_com_thoughtworks_mail_DominoDAO_nativeGetHeadPointer
(JNIEnv * jniEnv, jclass jcls, jstring serverName, jstring dbName, jstring password){
	char       *db_filename;    /* pathname of source database */
	char       *szServerName;
	char       *psd;
	MailMsg     *cp;
	STATUS      error = NOERROR;
	
	db_filename = jstringTostring(jniEnv, dbName);
	szServerName = jstringTostring(jniEnv, serverName);
	psd=jstringTostring(jniEnv, password);

	cp = (MailMsg *)malloc(LEN);
	cp->next=NULL;
	cp->from=NULL;
	
	if (NOERROR !=getMailMessages(szServerName,db_filename,psd,cp)){
	    destoryLinkList(cp);
	    free(db_filename);
	    free(szServerName);
	    free(psd);
	    return (jlong)(-1);
    }
	
	free(db_filename);
	free(szServerName);
	free(psd);

	return (jlong)cp;
}

JNIEXPORT jlong JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetNextPointer
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	if (((MailMsg *)peer)->next == NULL) {
		return 0;
	}else {
		return (jlong)(((MailMsg *)peer)->next);	
	}
}

JNIEXPORT jlong JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetNoteId
(JNIEnv * jniEnv, jobject jobj, jlong peer) {
	return (jlong)(*(((MailMsg *)peer)->id));	
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetSender
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->from);
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetReceivers
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->to);	
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetCopyTo
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->copyTo);	
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetBlindCopyTo
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->blindCopyTo);	
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetSubject
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->subject);
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetBody
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	return stoJstring(jniEnv, ((MailMsg *)peer)->body);
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetAttachments
  (JNIEnv * jniEnv, jobject jobj, jlong peer){
     return stoJstring(jniEnv,((MailMsg *)peer)->attachments);	
}

JNIEXPORT jstring JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeGetPostedDate
  (JNIEnv * jniEnv, jobject jobj, jlong peer){
     return stoJstring(jniEnv,((MailMsg *)peer)->postedDate);	
}

JNIEXPORT void JNICALL Java_com_thoughtworks_mail_MailWrapper_nativeDestroy
(JNIEnv * jniEnv, jobject jobj, jlong peer){
	MailMsg *cp = (MailMsg *)peer;
		
	if(peer!=NULL){
		if(cp->from != NULL){
			free(cp->id);
			free(cp->to);
			free(cp->subject);
			free(cp->copyTo);
			free(cp->blindCopyTo);
			free(cp->body);
			free(cp->from);
			free(cp->attachments);
			free(cp->postedDate);
		}
		free(cp);
	}
}

void destoryLinkList(MailMsg * peer){
    MailMsg *cp = ((MailMsg *)peer)->next;
    MailMsg * temp;

    while(cp!=NULL){
		free(cp->id);
		free(cp->to);
		free(cp->subject);
		free(cp->copyTo);
		free(cp->blindCopyTo);
		free(cp->body);
		free(cp->from);
		free(cp->attachments);
		free(cp->postedDate);
		temp = cp;
		cp=temp->next;
		free(temp);
	}
	free((MailMsg *)peer);

}

jstring stoJstring(JNIEnv *env, const char* pat)
{
	jclass strClass = env->FindClass("Ljava/lang/String;");
	jmethodID ctorID = env->GetMethodID(strClass, "<init>", "([BLjava/lang/String;)V");
	jbyteArray bytes = env->NewByteArray(strlen(pat));
	env->SetByteArrayRegion(bytes, 0, strlen(pat), (jbyte*)pat);
	jstring encoding = env->NewStringUTF("UTF-8");
	return (jstring)env->NewObject(strClass, ctorID, bytes, encoding);
}


char* jstringTostring(JNIEnv* env, jstring jstr)
{
   char* rtn = NULL;
   jclass clsstring = env->FindClass("java/lang/String");
   jstring strencode = env->NewStringUTF("utf-8");
   jmethodID mid = env->GetMethodID(clsstring, "getBytes", "(Ljava/lang/String;)[B");
   jbyteArray barr= (jbyteArray)env->CallObjectMethod(jstr, mid, strencode);
   jsize alen = env->GetArrayLength(barr);
   jbyte* ba = env->GetByteArrayElements(barr, JNI_FALSE);
   if (alen > 0)
   {
	 rtn = (char*)malloc(alen + 1);
	 memcpy(rtn, ba, alen);
	 rtn[alen] = 0;
   }
   env->ReleaseByteArrayElements(barr, ba, 0);
   return rtn;
} 

